// 实现这个项目的构建任务
const loadGruntTasks = require('load-grunt-tasks')
const sass = require('sass');
const { data } = require('./page.config')

module.exports = grunt => {
    grunt.initConfig({
        clean: ['dist/**'],


        // htmlmin 插件的配置信息
        htmlmin: {
            options: {
                removeComments: true,                     // 去除注释信息 
                collapseWhitespace: true,                   // 去除空白字符  
                removeEmptyAttributes: true,            // 去除标签的空属性
                removeCommentsFromCDATA: true, // 去除 CDATA 的注释信息
                removeRedundantAttributes: true     // 去除标签的冗余属性
            },
            // 具体任务配置
            build: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: '**/*.html',
                    dest: 'dist'
                }]
            }
        },
        sass: {
            options: {
                implementation: sass,
                sourceMap: true
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'src/assets/styles/',
                    src: '**/*.scss',
                    dest: 'dist/assets/styles/'
                }]

            }
        },
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            dist: {
                files: {
                    'dist/assets/script/main.js': 'src/assets/script/main.js'
                }
            }
        },
        imagemin: {
            dist: {
                options: {
                    optimizationLevel: 3 //定义 PNG 图片优化水平
                },
                files: [{
                    expand: true,
                    cwd: 'src/assets/images',   // 图片在imagemin目录下
                    src: ['**/**'], // 优化 imagemin 目录下所有 png/jpg/jpeg 图片
                    dest: 'src/assets/images/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },
        webfont: {
            icons: {
                src: 'src/assets/fonts/**',
                dest: 'dist/assets/fonts/**',
            }
        },
        watch: {
            js: {
                files: ['src/assets/script/*.js'],
                tasks: ['babel']
            },
            css: {
                files: ['src/assets/styles/*.scss'],
                tasks: ['sass']
            }
        },
        connect: {
            options: {
                port: 3000,
                hostname: '*', //默认就是这个值，可配置为本机某个 IP，localhost 或域名
                livereload: 35729  //声明给 watch 监听的端口
            },

            server: {
                options: {
                    open: true, //自动打开网页 http://
                    base: [
                        'app'  //主目录
                    ]
                }
            }
        },
    })

    loadGruntTasks(grunt) // 自动加载所有的 grunt 插件中的任务

    grunt.registerTask('build', ['clean', 'sass', 'babel'])
    grunt.registerTask('default', ['sass', 'babel', 'htmlmin', 'connect', 'watch'])
}